package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author mengWu
 * @Date 2021年05月21日 15:18
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    //格局医院编号和科室编号，查询科室名称
    @Override
    public String getDeptName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (null != department){
            return department.getDepname();
        }
        return null;
    }

    //上传科室接口
    @Override
    public void save(Map<String, Object> paramMap) {
        //将paramMap集合转换成对象
        Department department = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Department.class);

        //判断是否已经存在科室信息，医院可以重复，科室可以重复，但是医院中的科室不能重复
        //查询
        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        //判断查询的结果 --- 如果有进行更新操作，没有执行添加操作
        if (null != departmentExist) {
            //设置相关的默认值
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            //执行更新操作
            departmentRepository.save(departmentExist);
        } else {
            //设置相关参数的默认值
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            //执行添加
            departmentRepository.save(department);
        }

    }

    //分页查询科室数据
    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //创建Pageable对象，设置当前页，每页的记录数
        //0是第一页，需要page - 1
        Pageable pageable = PageRequest.of(page - 1, limit);
        //创建Example对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                //忽略大小写
                .withIgnoreCase(true);

        Example<Department> example = Example.of(department, matcher);

        Page<Department> all = departmentRepository.findAll(example, pageable);

        return all;
    }

    //删除科室接口实现
    @Override
    public void remove(String hoscode, String depcode) {
        /*
         * 根据医院编号和科室编号，查询数据,
         * 判断数据库中是否有数据，如果不存在数据就不需要删，如果有数据，根据id进行删除操作
         * @author mengWu
         * @date 2021/5/19 14:06
         * @param hoscode
         * @param depcode
         */
        //1、根据医院编号和科室编号，查询医院的信息
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        //2、判断
        if (department != null) {
            //调用删除方法执行删除操作 -- 根据id删除
            departmentRepository.deleteById(department.getId());
        }
    }

    //根据医院编号，查询医院科室数据
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        /*
         * 最终展示的结果是一个树型结构 -- 重要的
         * 1、创建一个List集合，用于最终数据的封装
         * @author mengWu
         * @date 2021/5/21 15:32
         * @param hoscode
         * @return java.util.List<com.atguigu.yygh.vo.hosp.DepartmentVo>
         */
        //创建list集合，用于最终的数据封装
        List<DepartmentVo> departmentVos = new ArrayList<>();

        //根据医院编号，从MongoDB中查询所有的科室数据
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example example = Example.of(departmentQuery);
        List<Department> departmentList = departmentRepository.findAll(example);
        //根据大科室编号分组，再获取每个大科室的下级科室
        Map<String, List<Department>> departmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigname));
        //遍历departmentMap集合
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()){
            //得到key和value
            //大科室编号
            String bigCode = entry.getKey();
            //大科室编号对应的全部数据
            List<Department> departments = entry.getValue();

            //封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigCode);
            departmentVo1.setDepname(departments.get(0).getBigname());
            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department : departments){
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());
                //封装到list集合中
                children.add(departmentVo2);
            }
            //将小科室的list集合，放到大科室的children里面
            departmentVo1.setChildren(children);
            //放到最终的result中
            departmentVos.add(departmentVo1);
        }
        //返回
        return departmentVos;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
